package com.battery.web.controller.system.battery;

import java.util.Date;
import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.DevApplyBO;
import com.battery.system.domain.DevApplyItems;
import com.battery.system.domain.VO.DevApplyVO;
import com.battery.system.service.battery.IDevApplyItemsService;
import com.battery.system.service.battery.IDevApplyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.DevApply;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 设备申领Controller
 *
 * @author battery
 * @date 2021-03-25
 */
@Controller
@RequestMapping("/system/devApply")
public class DevApplyController extends BaseController {
    private String prefix = "system/devApply";

    @Autowired
    private IDevApplyService devApplyService;

    @Autowired
    private IDevApplyItemsService devApplyItemsService;

    @RequiresPermissions("system:devApply:view")
    @GetMapping()
    public String devApply() {
        return prefix + "/devApply";
    }

    /**
     * 查询设备申领列表
     */
    @RequiresPermissions("system:devApply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DevApplyBO devApplyBO) {
        startPage();
//        List<DevApply> list = devApplyService.selectDevApplyList(devApply);
        List<DevApplyVO> list = devApplyService.selectDevApplyVOList(devApplyBO);
        return getDataTable(list);
    }


    @RequiresPermissions("system:devApply:list")
    @PostMapping("/itemslist")
    @ResponseBody
    public TableDataInfo itemslist(DevApplyItems devApplyItems) {
        startPage();
        List<DevApplyItems> list = devApplyItemsService.selectDevApplyItemsList(devApplyItems);
        return getDataTable(list);
    }

    /**
     * 导出设备申领列表
     */
    @RequiresPermissions("system:devApply:export")
    @Log(title = "设备申领", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DevApply devApply) {
        List<DevApply> list = devApplyService.selectDevApplyList(devApply);
        ExcelUtil<DevApply> util = new ExcelUtil<DevApply>(DevApply.class);
        return util.exportExcel(list, "devApply");
    }

    /**
     * 新增设备申领
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存设备申领
     */
    @RequiresPermissions("system:devApply:add")
    @Log(title = "设备申领", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DevApply devApply) {
        return toAjax(devApplyService.insertDevApply(devApply));
    }

    /**
     * 修改设备申领
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        DevApply devApply = devApplyService.selectDevApplyById(id);
        mmap.put("devApply", devApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备申领
     */
    @RequiresPermissions("system:devApply:edit")
    @Log(title = "设备申领", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DevApply devApply) {
        return toAjax(devApplyService.updateDevApply(devApply));
    }

    /**
     * 删除设备申领
     */
    @RequiresPermissions("system:devApply:remove")
    @Log(title = "设备申领", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(devApplyService.deleteDevApplyByIds(ids));
    }


    /**
     * 跳转到拒绝申请操作页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/refuseSp/{id}")
    public String refuseSp(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/refuseSp";
    }


    @Log(title = "设备申领-拒绝审批", businessType = BusinessType.OTHER)
    @RequiresPermissions("system:devApply:refuse")
    @PostMapping("/refuseSp")
    @ResponseBody
    public AjaxResult refuseSp(DevApply devApply) {
        if (devApply.getId() == null) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(devApply.getRemarks())) {
            return error("请填写拒绝理由");
        }
        /**
         * 审批进度
         * 0 未审批
         * 1 待发货
         * 2 已发货
         * 3 已拒绝
         */
        devApply.setStatus(3L);
        devApply.setApprovalTime(DateUtils.getNowDate());
        return toAjax(devApplyService.updateByPrimaryKeySelective(devApply));
    }

    @Log(title = "设备申领-审核通过", businessType = BusinessType.OTHER)
    @RequiresPermissions("system:devApply:adoptSh")
    @PostMapping("/adoptSh")
    @ResponseBody
    public AjaxResult adoptSh(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID为空");
        }

        Date date = DateUtils.getNowDate();
        DevApply devApplyUpdate = new DevApply();
        devApplyUpdate.setStatus(1L);// 1 待发货
        devApplyUpdate.setApprovalTime(date);
        devApplyUpdate.setUpdateTime(date);

        return toAjax(devApplyService.updateByPrimaryKeySelective(devApplyUpdate));
    }


    @Log(title = "设备申领-发货", businessType = BusinessType.OTHER)
    @RequiresPermissions("system:devApply:deliverGoods")
    @PostMapping("/deliverGoods")
    @ResponseBody
    public AjaxResult deliverGoods(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID为空");
        }

        Date date = DateUtils.getNowDate();
        DevApply devApplyUpdate = new DevApply();
        devApplyUpdate.setStatus(2L);
        devApplyUpdate.setDeliveryTime(date);
        devApplyUpdate.setUpdateTime(date);

        return toAjax(devApplyService.updateByPrimaryKeySelective(devApplyUpdate));
    }

}
